package com.nulldev.util.web.nTube.pages.misc;

import java.util.LinkedHashMap;
import java.util.Map;
import com.nulldev.util.VariableAPI.RandomUtil;

public class DJSCache {

	private static final int MAX = 96;
	private static final Map<String, String> cache = new LinkedHashMap<String, String>(MAX) {
		private static final long serialVersionUID = 2546245625L;

		@Override
		public String get(final Object key) {
			synchronized (this) {
				final String _o = super.remove(key);
				return _o;
			}
		}

		@Override
		protected boolean removeEldestEntry(final Map.Entry<String, String> oldest) {
			return super.size() > MAX;
		}

	};

	public static String allocateCache(final String payload) {
		final String id = RandomUtil.hexString(16);
		cache.put(id, payload);
		return id;
	}

	public static String get(final String id) {
		return cache.get(id);
	}

	public static String getNoRemove(final String id) {
		final String o = cache.get(id);
		cache.putIfAbsent(id, o);
		return o;
	}
}
